package org.example.juc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 张琪
 * @date 2024/4/18 15:18
 */
public class WaitNotifyExample {

    public static void main(String[] args) {
        WaitNotifyExample waitNotifyExample = new WaitNotifyExample();

        ExecutorService executorService = Executors.newFixedThreadPool(5);

        executorService.execute(waitNotifyExample::after);
        executorService.execute(waitNotifyExample::before);
    }
    public synchronized void before() {
        System.out.println("before");
        notifyAll();
    }


    public synchronized void after() {
        try {
            System.out.println("等待前");
            // wait 会挂起当前线程; 只有显式调用notify/notifyAll, 才能唤醒该线程
            // wait 会释放锁; sleep不会释放, 而是阻塞当前线程, 直到睡眠时间后才继续执行
            wait();
            System.out.println("等待后");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("after");
    }




}
